package com.itheima.controller;

import com.itheima.exception.MyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.ArrayList;

@Controller
public class ExceptionController {

    // 抛出空指针异常
    @RequestMapping("showNullPointer")
    public void showNullPointer() {
        ArrayList<Object> list = null;
        System.out.println(list.get(2)); // 这里会抛出空指针异常
    }

    // 抛出IO异常
    @RequestMapping("showIOException")
    public void showIOException() throws IOException {
        throw new IOException("这是一个IO异常示例");
    }

    // 抛出算术异常
    @RequestMapping("showArithmeticException")
    public void showArithmeticException() {
        int result = 10 / 0; // 这里会抛出算术异常
        System.out.println(result);
    }

    // 抛出自定义异常
    @RequestMapping("addData")
    public void addData() throws MyException {
        throw new MyException("新增数据异常！");
    }

    // 数组越界异常
    @RequestMapping("showIndexOutOfBounds")
    public void showIndexOutOfBounds() {
        int[] array = new int[5];
        System.out.println(array[10]); // 这里会抛出数组越界异常
    }
}